<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>JS基本语法之数组 | 小飞侠Pan的Blog</title><meta name="keywords" content="JS,数组"><meta name="author" content="潘静怡"><meta name="copyright" content="潘静怡"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="数组（Array）是属于内置对象。 一.创建数组对象方式一：使用字面量创建数组举例： 123var arr1 &#x3D; []; &#x2F;&#x2F; 创建一个空的数组var arr2 &#x3D; [1, 2, 3]; &#x2F;&#x2F; 创建带初始值的数组  方式二：使用构造函数创建数组语法： 123let arr &#x3D; new Array(参数);let arr &#x3D; Array(参数);  如果参数为空，则表示创建一个空数组；如果参数是一个">
<meta property="og:type" content="article">
<meta property="og:title" content="JS基本语法之数组">
<meta property="og:url" content="https://mengqiuleo.gitee.io/2022/02/26/js/%E6%95%B0%E7%BB%84/index.html">
<meta property="og:site_name" content="小飞侠Pan的Blog">
<meta property="og:description" content="数组（Array）是属于内置对象。 一.创建数组对象方式一：使用字面量创建数组举例： 123var arr1 &#x3D; []; &#x2F;&#x2F; 创建一个空的数组var arr2 &#x3D; [1, 2, 3]; &#x2F;&#x2F; 创建带初始值的数组  方式二：使用构造函数创建数组语法： 123let arr &#x3D; new Array(参数);let arr &#x3D; Array(参数);  如果参数为空，则表示创建一个空数组；如果参数是一个">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg">
<meta property="article:published_time" content="2022-02-26T10:47:39.357Z">
<meta property="article:modified_time" content="2022-02-26T10:58:59.495Z">
<meta property="article:author" content="潘静怡">
<meta property="article:tag" content="JS">
<meta property="article:tag" content="数组">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg"><link rel="shortcut icon" href="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252103634.jpg"><link rel="canonical" href="https://mengqiuleo.gitee.io/2022/02/26/js/%E6%95%B0%E7%BB%84/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":500},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'JS基本语法之数组',
  isPost: true,
  isHome: false,
  isHighlightShrink: undefined,
  isToc: true,
  postUpdate: '2022-02-26 18:58:59'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/social.css" type="text/css"><link rel="stylesheet" href="/css/style.css" type="text/css"><meta name="generator" content="Hexo 5.4.1"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252103634.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">15</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">20</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/book/"><i class="fa-fw fa-solid fa-bookmark"></i><span> 前端知识库</span></a></div><div class="menus_item"><a class="site-page" href="/work/"><i class="fa-fw fa fa-heartbeat"></i><span> 在线简历</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa-solid fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小飞侠Pan的Blog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/book/"><i class="fa-fw fa-solid fa-bookmark"></i><span> 前端知识库</span></a></div><div class="menus_item"><a class="site-page" href="/work/"><i class="fa-fw fa fa-heartbeat"></i><span> 在线简历</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa-solid fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">JS基本语法之数组</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-02-26T10:47:39.357Z" title="发表于 2022-02-26 18:47:39">2022-02-26</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-02-26T10:58:59.495Z" title="更新于 2022-02-26 18:58:59">2022-02-26</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/JS/">JS</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">8.2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>34分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="JS基本语法之数组"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div><article class="post-content" id="article-container"><p>数组（Array）是属于<strong>内置对象</strong>。</p>
<h4 id="一-创建数组对象"><a href="#一-创建数组对象" class="headerlink" title="一.创建数组对象"></a>一.创建数组对象</h4><h6 id="方式一：使用字面量创建数组"><a href="#方式一：使用字面量创建数组" class="headerlink" title="方式一：使用字面量创建数组"></a>方式一：使用字面量创建数组</h6><p>举例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr1 = []; <span class="comment">// 创建一个空的数组</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> arr2 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]; <span class="comment">// 创建带初始值的数组</span></span><br></pre></td></tr></table></figure>

<h6 id="方式二：使用构造函数创建数组"><a href="#方式二：使用构造函数创建数组" class="headerlink" title="方式二：使用构造函数创建数组"></a>方式二：使用构造函数创建数组</h6><p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = <span class="keyword">new</span> <span class="title class_">Array</span>(参数);</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> arr = <span class="title class_">Array</span>(参数);</span><br></pre></td></tr></table></figure>

<p>如果<strong>参数为空</strong>，则表示创建一个空数组；如果参数是<strong>一个数值</strong>时，表示数组的长度；如果有多个参数时，表示数组中的元素。</p>
<p>举例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 方式一</span></span><br><span class="line"><span class="keyword">var</span> arr1 = [<span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 方式二</span></span><br><span class="line"><span class="keyword">var</span> arr2 = <span class="keyword">new</span> <span class="title class_">Array</span>(); <span class="comment">// 参数为空</span></span><br><span class="line"><span class="keyword">var</span> arr3 = <span class="keyword">new</span> <span class="title class_">Array</span>(<span class="number">4</span>); <span class="comment">// 参数为一个数值</span></span><br><span class="line"><span class="keyword">var</span> arr4 = <span class="keyword">new</span> <span class="title class_">Array</span>(<span class="number">15</span>, <span class="number">16</span>, <span class="number">17</span>); <span class="comment">// 参数为多个数值</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> arr1); <span class="comment">// 打印结果：object</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr1 = &#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr1));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr2 = &#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr2));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr3 = &#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr3));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr4 = &#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr4));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">object;</span><br><span class="line"></span><br><span class="line">arr1 = [<span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>];</span><br><span class="line">arr2 = [];</span><br><span class="line">arr3 = [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>];</span><br><span class="line">arr4 = [<span class="number">15</span>, <span class="number">16</span>, <span class="number">17</span>];</span><br></pre></td></tr></table></figure>

<h6 id="Array-of"><a href="#Array-of" class="headerlink" title="Array.of"></a>Array.of</h6><p>使用<code>Array.of</code> 与 <code>new Array</code> 不同是设置一个参数时不会创建空元素数组</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = <span class="title class_">Array</span>.<span class="title function_">of</span>(<span class="number">3</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(hd); <span class="comment">//[3]</span></span><br><span class="line"></span><br><span class="line">arr = <span class="title class_">Array</span>.<span class="title function_">of</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(hd); <span class="comment">//[1, 2, 3]</span></span><br></pre></td></tr></table></figure>

<h4 id="二-数组中的元素的类型"><a href="#二-数组中的元素的类型" class="headerlink" title="二.数组中的元素的类型"></a>二.数组中的元素的类型</h4><p>数组中可以存放<strong>任意类型</strong>的数据，例如字符串、数字、布尔值、对象等。</p>
<p>比如：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="string">&#x27;qianguyihao&#x27;</span>, <span class="number">28</span>, <span class="literal">true</span>, &#123; <span class="attr">name</span>: <span class="string">&#x27;qianguyihao&#x27;</span> &#125;];</span><br></pre></td></tr></table></figure>

<p>还可以存放<strong>多维数组</strong>（数组里面放数组）。比如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr2 = [</span><br><span class="line">    [<span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>],</span><br><span class="line">    [<span class="number">21</span>, <span class="number">22</span>, <span class="number">23</span>],</span><br><span class="line">];</span><br></pre></td></tr></table></figure>

<h4 id="三-数组的常见方法"><a href="#三-数组的常见方法" class="headerlink" title="三.数组的常见方法"></a>三.数组的常见方法</h4><h5 id="1-数组的类型相关"><a href="#1-数组的类型相关" class="headerlink" title="1.数组的类型相关"></a>1.数组的类型相关</h5><table>
<thead>
<tr>
<th align="left">方法</th>
<th align="left">描述</th>
<th align="left">备注</th>
</tr>
</thead>
<tbody><tr>
<td align="left">Array.isArray()</td>
<td align="left">判断是否为数组</td>
<td align="left"></td>
</tr>
<tr>
<td align="left">toString()、String()</td>
<td align="left">将数组转换为字符串</td>
<td align="left"></td>
</tr>
<tr>
<td align="left">Array.from(arrayLike)</td>
<td align="left">将<strong>伪数组</strong>转化为<strong>真数组</strong></td>
<td align="left"></td>
</tr>
<tr>
<td align="left">Array.of(value1, value2, value3)</td>
<td align="left">创建数组：将<strong>一系列值</strong>转换成数组</td>
<td align="left"></td>
</tr>
</tbody></table>
<p>注意，获取数组的长度是用<code>length</code>属性，不是方法。</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//isArray()</span></span><br><span class="line"><span class="comment">//布尔值 = Array.isArray(被检测的数组);</span></span><br><span class="line"><span class="keyword">let</span> arr1=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">5</span>];</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Array</span>.<span class="title function_">isArray</span>(arr1));<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//toString()：数组转换为字符串</span></span><br><span class="line"><span class="comment">//字符串 = 数组.toString();</span></span><br><span class="line"><span class="comment">//解释：把数组转换成字符串，每一项用,分割。注：大多数的数据类型都可以使用.toString()方法，将其转换为字符串。</span></span><br><span class="line"><span class="keyword">let</span> str=arr1.<span class="title function_">toString</span>();</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str);<span class="comment">//1,2,5</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">String</span>(str));</span><br></pre></td></tr></table></figure>



<p>补充：将数组转换为字符串，有哪些方式</p>
<p>方式1：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">字符串 = 数组.<span class="title function_">toString</span>();</span><br></pre></td></tr></table></figure>

<p>方式 2：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">字符串 = <span class="title class_">String</span>(数组);</span><br></pre></td></tr></table></figure>

<p>方式 3：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">字符串 = 数组.<span class="title function_">join</span>(<span class="string">&#x27;,&#x27;</span>); <span class="comment">// 将数组转为字符串，每一项用 英文逗号 分隔</span></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>].<span class="title function_">join</span>(<span class="string">&quot;-&quot;</span>));<span class="comment">//1-2-3</span></span><br></pre></td></tr></table></figure>



<h6 id="Array-from-：将伪数组转换为真数组"><a href="#Array-from-：将伪数组转换为真数组" class="headerlink" title="Array.from()：将伪数组转换为真数组"></a>Array.from()：将伪数组转换为真数组</h6><p><strong>语法</strong>：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">array = <span class="title class_">Array</span>.<span class="title function_">from</span>(arrayLike);</span><br></pre></td></tr></table></figure>

<p><strong>作用</strong>：将<strong>伪数组</strong>或可遍历对象转换为<strong>真数组</strong>。</p>
<p>代码举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> name=<span class="string">&quot;xiaofeixiaPan&quot;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Array</span>.<span class="title function_">from</span>(name));</span><br><span class="line"><span class="comment">//(13) [&#x27;x&#x27;, &#x27;i&#x27;, &#x27;a&#x27;, &#x27;o&#x27;, &#x27;f&#x27;, &#x27;e&#x27;, &#x27;i&#x27;, &#x27;x&#x27;, &#x27;i&#x27;, &#x27;a&#x27;, &#x27;P&#x27;, &#x27;a&#x27;, &#x27;n&#x27;]</span></span><br></pre></td></tr></table></figure>

<p>注：<strong>伪数组与真数组的区别</strong></p>
<p><strong>伪数组</strong>：包含 length 属性的对象或可迭代的对象。</p>
<p>另外，伪数组的原型链中没有 Array.prototype，而真数组的原型链中有 Array.prototype。因此伪数组没有数组的一般方法，比如 pop()、join() 等方法。</p>
<h6 id="Array-of-：创建数组"><a href="#Array-of-：创建数组" class="headerlink" title="Array.of()：创建数组"></a>Array.of()：创建数组</h6><p><strong>语法</strong>：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Array</span>.<span class="title function_">of</span>(value1, value2, value3);</span><br></pre></td></tr></table></figure>

<p><strong>作用</strong>：根据参数里的内容，创建数组。</p>
<p><strong>举例</strong>：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = <span class="title class_">Array</span>.<span class="title function_">of</span>(<span class="number">1</span>, <span class="string">&#x27;abc&#x27;</span>, <span class="literal">true</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr); <span class="comment">// 打印结果是数组：[1, &quot;abc&quot;, true]</span></span><br></pre></td></tr></table></figure>

<p>注：<code>new Array()</code>和 <code>Array.of()</code>的区别在于：当参数只有一个时，前者表示数组的长度，后者表示数组中的内容。</p>
<h5 id="2-数组元素的添加和删除"><a href="#2-数组元素的添加和删除" class="headerlink" title="2.数组元素的添加和删除"></a>2.数组元素的添加和删除</h5><table>
<thead>
<tr>
<th align="left">方法</th>
<th align="left">描述</th>
<th align="left">备注</th>
</tr>
</thead>
<tbody><tr>
<td align="left">push()</td>
<td align="left">向数组的<strong>最后面</strong>插入一个或多个元素，返回结果为新数组的<strong>长度</strong></td>
<td align="left">会改变原数组</td>
</tr>
<tr>
<td align="left">pop()</td>
<td align="left">删除数组中的<strong>最后一个</strong>元素，返回结果为<strong>被删除的元素</strong></td>
<td align="left">会改变原数组</td>
</tr>
<tr>
<td align="left">unshift()</td>
<td align="left">在数组<strong>最前面</strong>插入一个或多个元素，返回结果为新数组的<strong>长度</strong></td>
<td align="left">会改变原数组</td>
</tr>
<tr>
<td align="left">shift()</td>
<td align="left">删除数组中的<strong>第一个</strong>元素，返回结果为<strong>被删除的元素</strong></td>
<td align="left">会改变原数组</td>
</tr>
<tr>
<td align="left">slice()</td>
<td align="left">从数组中<strong>提取</strong>指定的一个或多个元素，返回结果为<strong>新的数组</strong></td>
<td align="left">不会改变原数组</td>
</tr>
<tr>
<td align="left">splice()</td>
<td align="left">从数组中<strong>删除</strong>指定的一个或多个元素，返回结果为<strong>被删除元素组成的新数组</strong></td>
<td align="left">会改变原数组</td>
</tr>
<tr>
<td align="left">fill()</td>
<td align="left">填充数组：用固定的值填充数组，返回结果为<strong>新的数组</strong></td>
<td align="left">会改变原数组</td>
</tr>
</tbody></table>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//push()</span></span><br><span class="line"><span class="keyword">let</span> arr2=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line"><span class="keyword">let</span> result1=arr2.<span class="title function_">push</span>(<span class="number">5</span>);</span><br><span class="line"><span class="keyword">let</span> result2=arr2.<span class="title function_">push</span>(<span class="number">7</span>,<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result1);<span class="comment">//4</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result2);<span class="comment">//6</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr2));<span class="comment">//[1,2,3,5,7,0]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//pop()</span></span><br><span class="line"><span class="keyword">let</span> result3=arr2.<span class="title function_">pop</span>();</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result3);<span class="comment">//0 (返回结果为被删除的元素)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//unshift()</span></span><br><span class="line"><span class="keyword">let</span> result4=arr2.<span class="title function_">unshift</span>(<span class="string">&quot;hello&quot;</span>,<span class="string">&quot;world&quot;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result4);<span class="comment">//7</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr2));<span class="comment">//[&quot;hello&quot;,&quot;world&quot;,1,2,3,5,7]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//shift()</span></span><br><span class="line"><span class="keyword">let</span> result5=arr2.<span class="title function_">shift</span>();</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result5);<span class="comment">//hello</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr2));<span class="comment">//[&quot;world&quot;,1,2,3,5,7]</span></span><br></pre></td></tr></table></figure>

<h6 id="slice"><a href="#slice" class="headerlink" title="slice()"></a>slice()</h6><p>从数组中<strong>提取</strong>指定的一个或者多个元素，返回结果为<strong>新的数组</strong>（不会改变原来的数组）。</p>
<p>备注：该方法不会改变原数组，而是将截取到的元素封装到一个新数组中返回。</p>
<p><strong>语法</strong>：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">新数组 = 原数组.<span class="title function_">slice</span>(开始位置的索引, 结束位置的索引); <span class="comment">//注意：包含开始索引，不包含结束索引</span></span><br></pre></td></tr></table></figure>

<p>这里的索引是从下标为0开始</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> result1 = arr.<span class="title function_">slice</span>(); <span class="comment">// 不加参数时，则获取所有的元素。相当于数组的整体赋值</span></span><br><span class="line"><span class="keyword">const</span> result2 = arr.<span class="title function_">slice</span>(<span class="number">2</span>); <span class="comment">// 从第二个值开始提取，直到末尾</span></span><br><span class="line"><span class="keyword">const</span> result3 = arr.<span class="title function_">slice</span>(-<span class="number">2</span>); <span class="comment">// 提取最后两个元素</span></span><br><span class="line"><span class="keyword">const</span> result4 = arr.<span class="title function_">slice</span>(<span class="number">2</span>, <span class="number">4</span>); <span class="comment">// 提取从第二个到第四个之间的元素（不包括第四个元素）</span></span><br><span class="line"><span class="keyword">const</span> result5 = arr.<span class="title function_">slice</span>(<span class="number">4</span>, <span class="number">2</span>); <span class="comment">// 空</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result1:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result1));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result2:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result2));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result3:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result3));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result4:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result4));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result5:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result5));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">arr</span>: [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>];</span><br><span class="line"><span class="attr">result1</span>: [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>];</span><br><span class="line"><span class="attr">result2</span>: [<span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>];</span><br><span class="line"><span class="attr">result3</span>: [<span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>];</span><br><span class="line"><span class="attr">result4</span>: [<span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>];</span><br><span class="line"><span class="attr">result5</span>: [];</span><br></pre></td></tr></table></figure>

<h6 id="splice"><a href="#splice" class="headerlink" title="splice()"></a>splice()</h6><p><code>splice()</code>：从数组中<strong>删除</strong>指定的一个或多个元素，返回结果为<strong>被删除元素组成的新数组</strong>（会改变原来的数组）。</p>
<p>备注：该方法会改变原数组，会将指定元素从原数组中删除；被删除的元素会封装到一个新的数组中返回。</p>
<p>语法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">新数组 = 原数组.<span class="title function_">splice</span>(起始索引index, 需要删除的个数);</span><br><span class="line"></span><br><span class="line">新数组 = 原数组.<span class="title function_">splice</span>(起始索引index, 需要删除的个数, 新的元素<span class="number">1</span>, 新的元素<span class="number">2.</span>..);</span><br></pre></td></tr></table></figure>

<p>上方语法中，第三个及之后的参数，表示：删除元素之后，向原数组中添加新的元素，这些元素将会自动插入到起始位置索引的前面。也可以理解成：删除了哪些元素，就在那些元素的所在位置补充新的内容。</p>
<p>举例 1：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr1 = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>];</span><br><span class="line"><span class="keyword">var</span> result1 = arr1.<span class="title function_">splice</span>(<span class="number">1</span>); <span class="comment">//从第index为1的位置开始，删除元素，删到末尾</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr1：&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr1));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result1：&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result1));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">arr1：[<span class="string">&quot;a&quot;</span>]</span><br><span class="line">result1：[<span class="string">&quot;b&quot;</span>,<span class="string">&quot;c&quot;</span>,<span class="string">&quot;d&quot;</span>,<span class="string">&quot;e&quot;</span>,<span class="string">&quot;f&quot;</span>]</span><br></pre></td></tr></table></figure>

<p>举例 2：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr2 = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>];</span><br><span class="line"><span class="keyword">var</span> result2 = arr2.<span class="title function_">splice</span>(-<span class="number">2</span>); <span class="comment">//删除最后两个元素</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr2：&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr2));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result2：&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result2));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">arr2：[<span class="string">&quot;a&quot;</span>,<span class="string">&quot;b&quot;</span>,<span class="string">&quot;c&quot;</span>,<span class="string">&quot;d&quot;</span>]</span><br><span class="line">result2：[<span class="string">&quot;e&quot;</span>,<span class="string">&quot;f&quot;</span>]</span><br></pre></td></tr></table></figure>

<p>举例 3：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr3 = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>];</span><br><span class="line"><span class="keyword">var</span> result3 = arr3.<span class="title function_">splice</span>(<span class="number">1</span>, <span class="number">3</span>); <span class="comment">//从第index为1的位置开始删除元素,一共删除三个元素</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr3：&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr3));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result3：&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result3));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">arr3：[<span class="string">&quot;a&quot;</span>,<span class="string">&quot;e&quot;</span>,<span class="string">&quot;f&quot;</span>]</span><br><span class="line">result3：[<span class="string">&quot;b&quot;</span>,<span class="string">&quot;c&quot;</span>,<span class="string">&quot;d&quot;</span>]</span><br></pre></td></tr></table></figure>

<p>举例4：（删除指定元素）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr4 = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>];</span><br><span class="line">arr4.<span class="title function_">splice</span>(arr4.<span class="title function_">indexOf</span>(<span class="string">&#x27;c&#x27;</span>), <span class="number">1</span>); <span class="comment">// 删除数组中的&#x27;c&#x27;这个元素</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr4：&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr4));</span><br></pre></td></tr></table></figure>

<p>举例 5：（<strong>第三个参数</strong>的用法）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr5 = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">//从第index为1的位置开始删除元素,一共删除三个元素。并且在 index=1 的前面追加两个元素</span></span><br><span class="line"><span class="keyword">var</span> result5 = arr5.<span class="title function_">splice</span>(<span class="number">1</span>, <span class="number">3</span>, <span class="string">&#x27;xiaofeixia&#x27;</span>, <span class="string">&#x27;Pan&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr5&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr5));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result4：&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result5));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">arr5[<span class="string">&quot;a&quot;</span>,<span class="string">&quot;xiaofeixia&quot;</span>,<span class="string">&quot;Pan&quot;</span>,<span class="string">&quot;e&quot;</span>,<span class="string">&quot;f&quot;</span>]</span><br><span class="line">result5[<span class="string">&quot;b&quot;</span>,<span class="string">&quot;c&quot;</span>,<span class="string">&quot;d&quot;</span>]</span><br></pre></td></tr></table></figure>

<h6 id="fill"><a href="#fill" class="headerlink" title="fill()"></a>fill()</h6><p>用一个固定值填充数组，返回结果为<strong>新的数组</strong>。会改变原数组。</p>
<p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 用一个固定值填充数组。数组里的每个元素都会被这个固定值填充</span></span><br><span class="line">新数组 = 数组.<span class="title function_">fill</span>(固定值);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 从 startIndex 开始的数组元素，用固定值填充</span></span><br><span class="line">新数组 = 数组.<span class="title function_">fill</span>(固定值, startIndex);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 从 startIndex 到 endIndex 之间的元素（包左不包右），用固定值填充</span></span><br><span class="line">新数组 = 数组.<span class="title function_">fill</span>(固定值, startIndex, endIndex);</span><br></pre></td></tr></table></figure>

<p>举例1：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建一个长度为4的空数组，然后用 &#x27;f&#x27; 来填充这个空数组</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Array</span>(<span class="number">4</span>).<span class="title function_">fill</span>(<span class="string">&#x27;f&#x27;</span>)); <span class="comment">// [&#x27;f&#x27;, &#x27;f&#x27;, &#x27;f,&#x27; &#x27;f&#x27;]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 将现有数组的每一个元素都进行填充</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>([<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>].<span class="title function_">fill</span>(<span class="string">&#x27;f&#x27;</span>)); <span class="comment">// [&#x27;f&#x27;, &#x27;f&#x27;, &#x27;f,&#x27; &#x27;f&#x27;]</span></span><br></pre></td></tr></table></figure>

<p>举例2：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 指定位置进行填充</span></span><br><span class="line"><span class="keyword">let</span> arr1 = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>];</span><br><span class="line"><span class="keyword">let</span> arr2 = arr1.<span class="title function_">fill</span>(<span class="string">&#x27;f&#x27;</span>, <span class="number">1</span>, <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr1); <span class="comment">// [&#x27;a&#x27;, &#x27;f&#x27;, &#x27;f,&#x27; &#x27;d&#x27;]</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr2); <span class="comment">// [&#x27;a&#x27;, &#x27;f&#x27;, &#x27;f,&#x27; &#x27;d&#x27;]</span></span><br></pre></td></tr></table></figure>



<h5 id="3-数组的合并和拆分"><a href="#3-数组的合并和拆分" class="headerlink" title="3.数组的合并和拆分"></a>3.数组的合并和拆分</h5><table>
<thead>
<tr>
<th align="left">方法</th>
<th align="left">描述</th>
<th align="left">备注</th>
</tr>
</thead>
<tbody><tr>
<td align="left">concat()</td>
<td align="left">合并数组：连接两个或多个数组，返回结果为<strong>新的数组</strong></td>
<td align="left">不会改变原数组</td>
</tr>
<tr>
<td align="left">join()</td>
<td align="left">将数组转换为字符串，返回结果为<strong>转换后的字符串</strong></td>
<td align="left">不会改变原数组</td>
</tr>
<tr>
<td align="left">split()</td>
<td align="left">将字符串按照指定的分隔符，组装为数组</td>
<td align="left">不会改变原字符串</td>
</tr>
<tr>
<td align="left">copyWith()</td>
<td align="left">从数组中复制一部分到同数组中的另外位置</td>
<td align="left">会改变原数组</td>
</tr>
</tbody></table>
<p>注意，<code>split()</code>是字符串的方法，不是数组的方法。</p>
<h6 id="concat"><a href="#concat" class="headerlink" title="concat()"></a>concat()</h6><p><code>concat()</code>：连接两个或多个数组，返回结果为<strong>新的数组</strong>。不会改变原数组。<code>concat()</code>方法的作用是<strong>数组合并</strong>。</p>
<p>语法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">新数组 = 数组<span class="number">1.</span><span class="title function_">concat</span>(数组<span class="number">2</span>, 数组<span class="number">3</span> ...);</span><br></pre></td></tr></table></figure>

<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">const</span> arr2 = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>];</span><br><span class="line"><span class="keyword">const</span> arr3 = [<span class="string">&#x27;xiaofeixia&#x27;</span>, <span class="string">&#x27;Pan&#x27;</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> result1 = arr1.<span class="title function_">concat</span>(arr2);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> result2 = arr2.<span class="title function_">concat</span>(arr1, arr3);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr1 =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr1));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr2 =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr2));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr3 =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr3));</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result1 =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result1));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result2 =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result2));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">arr1 =[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">arr2 =[<span class="string">&quot;a&quot;</span>,<span class="string">&quot;b&quot;</span>,<span class="string">&quot;c&quot;</span>]</span><br><span class="line">arr3 =[<span class="string">&quot;xiaofeixia&quot;</span>,<span class="string">&quot;Pan&quot;</span>]</span><br><span class="line"></span><br><span class="line">result1 =[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="string">&quot;a&quot;</span>,<span class="string">&quot;b&quot;</span>,<span class="string">&quot;c&quot;</span>]</span><br><span class="line">result2 =[<span class="string">&quot;a&quot;</span>,<span class="string">&quot;b&quot;</span>,<span class="string">&quot;c&quot;</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="string">&quot;xiaofeixia&quot;</span>,<span class="string">&quot;Pan&quot;</span>]</span><br></pre></td></tr></table></figure>

<p><strong>数组合并的另一种方式</strong>：</p>
<p>我们可以使用<code>...</code>这种展开语法，将两个数组进行合并。举例如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> result = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, ...arr1];</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result)); <span class="comment">// 打印结果：[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,1,2,3]</span></span><br></pre></td></tr></table></figure>

<h6 id="join"><a href="#join" class="headerlink" title="join()"></a>join()</h6><p><code>join()</code>：将数组转换为字符串，返回结果为<strong>转换后的字符串</strong>（不会改变原来的数组）。</p>
<p>补充：<code>join()</code>方法可以指定一个<strong>字符串</strong>作为参数，这个字符串将会成为数组中元素的<strong>连接符</strong>；如果不指定连接符，则默认使用 <code>,</code> 作为连接符，此时和 <code>toString()的效果是一致的</code>。</p>
<p>语法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">新的字符串 = 原数组.<span class="title function_">join</span>(参数); <span class="comment">// 参数选填</span></span><br></pre></td></tr></table></figure>

<p>代码举例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> result1 = arr.<span class="title function_">join</span>(); <span class="comment">// 这里没有指定连接符，所以默认使用 , 作为连接符</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> result2 = arr.<span class="title function_">join</span>(<span class="string">&#x27;-&#x27;</span>); <span class="comment">// 使用指定的字符串作为连接符</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> arr); <span class="comment">// 打印结果：object</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> result1); <span class="comment">// 打印结果：string</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result1 =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result1));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result2 =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result2));</span><br></pre></td></tr></table></figure>

<p>上方代码中，最后三行的打印结果是：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">arr =[<span class="string">&quot;a&quot;</span>,<span class="string">&quot;b&quot;</span>,<span class="string">&quot;c&quot;</span>]</span><br><span class="line">result1 =a,b,c</span><br><span class="line">result2 =a-b-c</span><br></pre></td></tr></table></figure>

<h6 id="split"><a href="#split" class="headerlink" title="split()"></a>split()</h6><blockquote>
<p>注意，<code>split()</code>是字符串的方法，不是数组的方法。</p>
</blockquote>
<p>语法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">新的数组 = str.<span class="title function_">split</span>(分隔符);</span><br></pre></td></tr></table></figure>

<p>解释：通过指定的分隔符，将一个字符串拆分成一个<strong>数组</strong>。不会改变原字符串。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str2=<span class="string">&quot;h,e,l,l,o&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> arr5=str2.<span class="title function_">split</span>(<span class="string">&#x27;,&#x27;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr5);<span class="comment">//(5) [&#x27;h&#x27;, &#x27;e&#x27;, &#x27;l&#x27;, &#x27;l&#x27;, &#x27;o&#x27;]</span></span><br></pre></td></tr></table></figure>

<h6 id="copyWithin"><a href="#copyWithin" class="headerlink" title="copyWithin()"></a>copyWithin()</h6><p>使用 <code>copyWithin</code> 从数组中复制一部分到同数组中的另外位置。</p>
<p>语法说明</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">array.copyWithin(target, start, end)</span><br></pre></td></tr></table></figure>

<p>参数说明</p>
<table>
<thead>
<tr>
<th align="left">参数</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><em>target</em></td>
<td align="left">必需。复制到指定目标索引位置。</td>
</tr>
<tr>
<td align="left"><em>start</em></td>
<td align="left">可选。元素复制的起始位置。</td>
</tr>
<tr>
<td align="left"><em>end</em></td>
<td align="left">可选。停止复制的索引位置 (默认为 <em>array</em>.length)。如果为负值，表示倒数。</td>
</tr>
</tbody></table>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>];</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">copyWithin</span>(<span class="number">2</span>, <span class="number">0</span>, <span class="number">2</span>)); <span class="comment">//[1, 2, 1, 2]</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr);<span class="comment">//(4) [1, 2, 1, 2]</span></span><br></pre></td></tr></table></figure>

<h5 id="4-数组排序"><a href="#4-数组排序" class="headerlink" title="4.数组排序"></a>4.数组排序</h5><table>
<thead>
<tr>
<th align="left">方法</th>
<th align="left">描述</th>
<th align="left">备注</th>
</tr>
</thead>
<tbody><tr>
<td align="left">reverse()</td>
<td align="left">反转数组，返回结果为<strong>反转后的数组</strong></td>
<td align="left">会改变原数组</td>
</tr>
<tr>
<td align="left">sort()</td>
<td align="left">对数组的元素,默认按照<strong>Unicode 编码</strong>，从小到大进行排序</td>
<td align="left">会改变原数组</td>
</tr>
</tbody></table>
<h6 id="reverse"><a href="#reverse" class="headerlink" title="reverse()"></a>reverse()</h6><p><code>reverse()</code>：反转数组，返回结果为<strong>反转后的数组</strong>（会改变原来的数组）。</p>
<p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">反转后的数组 = 数组.<span class="title function_">reverse</span>();</span><br></pre></td></tr></table></figure>

<p>举例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> result = arr.<span class="title function_">reverse</span>(); <span class="comment">// 将数组 arr 进行反转</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">arr =[<span class="string">&quot;f&quot;</span>,<span class="string">&quot;e&quot;</span>,<span class="string">&quot;d&quot;</span>,<span class="string">&quot;c&quot;</span>,<span class="string">&quot;b&quot;</span>,<span class="string">&quot;a&quot;</span>]</span><br><span class="line">result =[<span class="string">&quot;f&quot;</span>,<span class="string">&quot;e&quot;</span>,<span class="string">&quot;d&quot;</span>,<span class="string">&quot;c&quot;</span>,<span class="string">&quot;b&quot;</span>,<span class="string">&quot;a&quot;</span>]</span><br></pre></td></tr></table></figure>

<h6 id="sort-方法"><a href="#sort-方法" class="headerlink" title="sort()方法"></a>sort()方法</h6><p>1.sort()方法：<strong>无参时</strong></p>
<p>如果在使用 sort() 方法时不带参，则默认按照<strong>Unicode 编码</strong>，从小到大进行排序。</p>
<p><strong>举例 1</strong>：（当数组中的元素为字符串时）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr1 = [<span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;f&#x27;</span>, <span class="string">&#x27;c&#x27;</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> result = arr1.<span class="title function_">sort</span>(); <span class="comment">// 将数组 arr1 进行排序</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr1 =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr1));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">arr1 =[<span class="string">&quot;a&quot;</span>,<span class="string">&quot;b&quot;</span>,<span class="string">&quot;c&quot;</span>,<span class="string">&quot;d&quot;</span>,<span class="string">&quot;e&quot;</span>,<span class="string">&quot;f&quot;</span>]</span><br><span class="line">result =[<span class="string">&quot;a&quot;</span>,<span class="string">&quot;b&quot;</span>,<span class="string">&quot;c&quot;</span>,<span class="string">&quot;d&quot;</span>,<span class="string">&quot;e&quot;</span>,<span class="string">&quot;f&quot;</span>]</span><br></pre></td></tr></table></figure>

<p><strong>举例 2</strong>：（当数组中的元素为数字时）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr2 = [<span class="number">5</span>, <span class="number">2</span>, <span class="number">11</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">1</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> result = arr2.<span class="title function_">sort</span>(); <span class="comment">// 将数组 arr2 进行排序</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr2 =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr2));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">arr2 =[<span class="number">1</span>,<span class="number">11</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line">result =[<span class="number">1</span>,<span class="number">11</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br></pre></td></tr></table></figure>

<p>使用 sort() 排序后，数字<code>11</code>竟然在数字<code>2</code>的前面。</p>
<p>原因：<code>sort()</code>方法是按照<strong>Unicode 编码</strong>进行排序的。</p>
<p>2.sort()方法：<strong>带参时</strong>，自定义排序规则</p>
<p>如果在 sort()方法中带参，可以<strong>自定义</strong>排序规则。具体做法如下：</p>
<p>可以在 sort()添加一个回调函数，来指定排序规则。回调函数中需要定义两个形参，浏览器将会分别使用数组中的元素作为实参去调用回调函数。</p>
<p>浏览器根据回调函数的返回值来决定元素的排序：（重要）</p>
<ul>
<li>如果返回一个大于 0 的值，则元素会交换位置</li>
<li><strong>如果返回一个小于 0 的值，则元素位置不变</strong></li>
<li>如果返回一个等于 0 的值，则认为两个元素相等，则不交换位置</li>
</ul>
<p><strong>写法 1</strong>：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">5</span>, <span class="number">2</span>, <span class="number">11</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">1</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 自定义排序规则</span></span><br><span class="line"><span class="keyword">var</span> result = arr.<span class="title function_">sort</span>(<span class="keyword">function</span> (<span class="params">a, b</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (a &gt; b) &#123;</span><br><span class="line">        <span class="comment">// 如果 a 大于 b，则交换 a 和 b 的位置</span></span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (a &lt; b) &#123;</span><br><span class="line">        <span class="comment">// 如果 a 小于 b，则位置不变</span></span><br><span class="line">        <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 如果 a 等于 b，则位置不变</span></span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result));</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">11</span>];</span><br><span class="line">result = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">11</span>];</span><br></pre></td></tr></table></figure>

<p><strong>写法 2</strong>：（冒泡排序）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">5</span>, <span class="number">2</span>, <span class="number">11</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">1</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 自定义排序规则</span></span><br><span class="line"><span class="keyword">let</span> result = arr.<span class="title function_">sort</span>(<span class="keyword">function</span> (<span class="params">a, b</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> a - b; <span class="comment">// 升序排列</span></span><br><span class="line">    <span class="comment">// return b - a; // 降序排列</span></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result));</span><br></pre></td></tr></table></figure>

<p>打印结果不变。</p>
<p><strong>写法 3</strong>：（箭头函数）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">5</span>, <span class="number">2</span>, <span class="number">11</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">1</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 自定义排序规则</span></span><br><span class="line"><span class="keyword">let</span> result = arr.<span class="title function_">sort</span>(<span class="function">(<span class="params">a, b</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> a - b; <span class="comment">// 升序排列</span></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result));</span><br></pre></td></tr></table></figure>

<p><strong>写法 4</strong>：（推荐）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">5</span>, <span class="number">2</span>, <span class="number">11</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">1</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 自定义排序规则：升序排列</span></span><br><span class="line"><span class="keyword">let</span> result = arr.<span class="title function_">sort</span>(<span class="function">(<span class="params">a, b</span>) =&gt;</span> a - b);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result =&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(result));</span><br></pre></td></tr></table></figure>



<h5 id="5-查找数组的元素"><a href="#5-查找数组的元素" class="headerlink" title="5.查找数组的元素"></a>5.查找数组的元素</h5><table>
<thead>
<tr>
<th align="left">方法</th>
<th align="left">描述</th>
<th align="left">备注</th>
</tr>
</thead>
<tbody><tr>
<td align="left">indexOf(value)</td>
<td align="left">从前往后索引，检索一个数组中是否含有指定的元素</td>
<td align="left"></td>
</tr>
<tr>
<td align="left">lastIndexOf(value)</td>
<td align="left">从后往前索引，检索一个数组中是否含有指定的元素</td>
<td align="left"></td>
</tr>
<tr>
<td align="left">includes(item)</td>
<td align="left">数组中是否包含指定的内容</td>
<td align="left"></td>
</tr>
<tr>
<td align="left">find(function())</td>
<td align="left">找出<strong>第一个</strong>满足「指定条件返回 true」的元素</td>
<td align="left"></td>
</tr>
<tr>
<td align="left">findIndex(function())</td>
<td align="left">找出<strong>第一个</strong>满足「指定条件返回 true」的元素的 index</td>
<td align="left"></td>
</tr>
<tr>
<td align="left">every()</td>
<td align="left">确保数组中的每个元素都满足「指定条件返回 true」，则停止遍历，此方法才返回 true</td>
<td align="left">全真才为真。要求每一项都返回 true，最终的结果才返回 true</td>
</tr>
<tr>
<td align="left">some()</td>
<td align="left">数组中只要有一个元素满足「指定条件返回 true」，则停止遍历，此方法就返回 true</td>
<td align="left">一真即真。只要有一项返回 true，最终的结果就返回 true</td>
</tr>
</tbody></table>
<h6 id="indexOf-和-lastIndexOf-：获取数据的索引"><a href="#indexOf-和-lastIndexOf-：获取数据的索引" class="headerlink" title="indexOf() 和 lastIndexOf()：获取数据的索引"></a>indexOf() 和 lastIndexOf()：获取数据的索引</h6><p><strong>语法 1</strong>：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">索引值 = 数组.<span class="title function_">indexOf</span>(想要查询的元素);</span><br><span class="line"></span><br><span class="line">索引值 = 数组.<span class="title function_">lastIndexOf</span>(想要查询的元素);</span><br></pre></td></tr></table></figure>

<p>备注：<code>indexOf()</code> 是从前向后查找元素的位置。同理，<code>lastIndexOf()</code>是从后向前寻找。</p>
<p><strong>解释</strong>：可以检索一个数组中是否含有指定的元素。如果数组中含有该元素，则会返回其<strong>第一次出现</strong>的索引；如果没有找到指定的内容，则返回 -1。</p>
<p>这个方法的作用：</p>
<ul>
<li><strong>如果获取的索引值为 0，说明数组是以查询的元素为开头的</strong>。</li>
<li>如果获取的索引值为-1，说明这个数组中没有指定的元素。</li>
</ul>
<p><strong>注意</strong>：<code>indexOf()</code>在检索时，是严格类型约束，类似于<code>===</code>。</p>
<p><strong>举例 1</strong>：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;c&#x27;</span>];</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">indexOf</span>(<span class="string">&#x27;c&#x27;</span>)); <span class="comment">//从前往后，找第一个&quot;c&quot;在哪个位置</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">lastIndexOf</span>(<span class="string">&#x27;d&#x27;</span>)); <span class="comment">//从后往前，找第一个&quot;d&quot;在哪个位置</span></span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">2</span><br><span class="line">5</span><br></pre></td></tr></table></figure>

<p><strong>举例 2</strong>：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;5&#x27;</span>];</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">indexOf</span>(<span class="number">2</span>));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">-1</span><br></pre></td></tr></table></figure>



<p><strong>语法 2</strong>：</p>
<p>这个方法还可以指定第二个参数，用来指定查找的<strong>起始位置</strong>。语法如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">索引值 = 数组.<span class="title function_">indexOf</span>(想要查询的元素, [起始位置]);</span><br></pre></td></tr></table></figure>

<p>举例 3：（两个参数时，需要特别注意）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="string">&#x27;x&#x27;</span>, <span class="string">&#x27;i&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;o&#x27;</span>, <span class="string">&#x27;f&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;i&#x27;</span>, <span class="string">&#x27;x&#x27;</span>, <span class="string">&#x27;i&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;P&#x27;</span>,<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;n&#x27;</span>];</span><br><span class="line">result = arr.<span class="title function_">indexOf</span>(<span class="string">&#x27;i&#x27;</span>, <span class="number">3</span>); <span class="comment">// 从下标为3的位置开始查找 &#x27;i&#x27;这个元素 </span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result); <span class="comment">// 打印结果：6</span></span><br></pre></td></tr></table></figure>

<h6 id="includes"><a href="#includes" class="headerlink" title="includes()"></a>includes()</h6><p><strong>语法</strong>：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">布尔值 = arr.<span class="title function_">includes</span>(想要查找的元素, [position]);</span><br></pre></td></tr></table></figure>

<p><strong>解释</strong>：判断一个数组中是否包含指定的元素。如果是，则会返回 true；否则返回 false。</p>
<p>参数中的 <code>position</code>：如果不指定，则默认为0；如果指定，则规定了检索的起始位置。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>, <span class="number">15</span>];</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">includes</span>(<span class="number">12</span>)); <span class="comment">// 打印结果：true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">includes</span>(<span class="number">20</span>)); <span class="comment">// 打印结果：false</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">includes</span>(<span class="number">11</span>, <span class="number">1</span>)); <span class="comment">// 打印结果：false</span></span><br></pre></td></tr></table></figure>

<h6 id="find"><a href="#find" class="headerlink" title="find()"></a>find()</h6><p><strong>语法</strong>：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">find</span>(<span class="function">(<span class="params">item, index,arr</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p><strong>作用</strong>：找出<strong>第一个</strong>满足「指定条件返回 true」的元素；如果没找到，则返回 undefined。</p>
<p>备注：一旦找到符合条件的第一个元素，将不再继续往下遍历。</p>
<p>举例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">2</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">6</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> result = arr.<span class="title function_">find</span>(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> item &gt; <span class="number">4</span>; <span class="comment">//遍历数组arr，一旦发现有第一个元素大于4，就把这个元素返回</span></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result); <span class="comment">//打印结果：5</span></span><br></pre></td></tr></table></figure>

<h6 id="findIndex"><a href="#findIndex" class="headerlink" title="findIndex()"></a>findIndex()</h6><p><strong>语法</strong>：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">findIndex</span>(<span class="function">(<span class="params">item, index, arr</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p><strong>作用</strong>：找出<strong>第一个</strong>满足「指定条件返回 true」的元素的 index。</p>
<p>举例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">2</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">6</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> result = arr.<span class="title function_">findIndex</span>(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> item &gt; <span class="number">4</span>; <span class="comment">//遍历数组arr，一旦发现有第一个元素大于4，就把这个元素的index返回</span></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result); <span class="comment">//打印结果：3</span></span><br></pre></td></tr></table></figure>

<h6 id="every"><a href="#every" class="headerlink" title="every()"></a>every()</h6><p><code>every()</code>：对数组中每一项运行回调函数，如果都返回 true，every 就返回 true；如果有一项返回 false，则停止遍历，此方法返回 false。</p>
<p>注意：every()方法的返回值是 boolean 值，参数是回调函数。</p>
<p>举例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr1 = [<span class="string">&#x27;好好&#x27;</span>, <span class="string">&#x27;学习&#x27;</span>, <span class="string">&#x27;天天向上&#x27;</span>, <span class="string">&#x27;张三&#x27;</span>];</span><br><span class="line"><span class="keyword">var</span> bool1 = arr1.<span class="title function_">every</span>(<span class="keyword">function</span> (<span class="params">element, index, array</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (element.<span class="property">length</span> &gt; <span class="number">2</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(bool1); <span class="comment">//输出结果：false。只要有一个元素的长度是超过两个字符的，就返回false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> arr2 = [<span class="string">&#x27;好好&#x27;</span>, <span class="string">&#x27;学习&#x27;</span>, <span class="string">&#x27;天天&#x27;</span>, <span class="string">&#x27;向上&#x27;</span>];</span><br><span class="line"><span class="keyword">var</span> bool2 = arr2.<span class="title function_">every</span>(<span class="keyword">function</span> (<span class="params">element, index, array</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (element.<span class="property">length</span> &gt; <span class="number">2</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(bool2); <span class="comment">//输出结果：true。因为每个元素的长度都是两个字符。</span></span><br></pre></td></tr></table></figure>

<h6 id="some"><a href="#some" class="headerlink" title="some()"></a>some()</h6><p><code>some()</code>：对数组中每一个元素运行回调函数，只要有一个元素返回 true，则停止遍历，此方法返回 true。</p>
<p>注意：some()方法的返回值是 boolean 值。</p>
<h5 id="6-遍历数组"><a href="#6-遍历数组" class="headerlink" title="6.遍历数组"></a>6.遍历数组</h5><table>
<thead>
<tr>
<th align="left">方法</th>
<th align="left">描述</th>
<th align="left">备注</th>
</tr>
</thead>
<tbody><tr>
<td align="left">for 循环</td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr>
<td align="left">forEach()</td>
<td align="left">和 for 循环类似，但需要兼容 IE8 以上</td>
<td align="left">forEach() 没有返回值。也就是说，它的返回值是 undefined</td>
</tr>
<tr>
<td align="left">map()</td>
<td align="left">对原数组中的每一项进行加工，将组成新的数组</td>
<td align="left">不会改变原数组</td>
</tr>
<tr>
<td align="left">filter()</td>
<td align="left">过滤数组：返回结果是 true 的项，将组成新的数组，返回结果为<strong>新的数组</strong></td>
<td align="left">不会改变原数组</td>
</tr>
<tr>
<td align="left">reduce</td>
<td align="left">接收一个函数作为累加器，返回值是回调函数累计处理的结果</td>
<td align="left"></td>
</tr>
<tr>
<td align="left">for/in、for/of</td>
<td align="left"></td>
<td align="left"></td>
</tr>
</tbody></table>
<h6 id="for-in"><a href="#for-in" class="headerlink" title="for/in"></a>for/in</h6><p>遍历时的 key 值为数组的索引</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> lessons = [</span><br><span class="line">	&#123;<span class="attr">title</span>: <span class="string">&#x27;媒体查询响应式布局&#x27;</span>,<span class="attr">category</span>: <span class="string">&#x27;css&#x27;</span>&#125;,</span><br><span class="line">  &#123;<span class="attr">title</span>: <span class="string">&#x27;FLEX 弹性盒模型&#x27;</span>,<span class="attr">category</span>: <span class="string">&#x27;css&#x27;</span>&#125;,</span><br><span class="line">	&#123;<span class="attr">title</span>: <span class="string">&#x27;MYSQL多表查询随意操作&#x27;</span>,<span class="attr">category</span>: <span class="string">&#x27;mysql&#x27;</span>&#125;</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> key <span class="keyword">in</span> lessons) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`标题: <span class="subst">$&#123;lessons[key].title&#125;</span>`</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">标题: 媒体查询响应式布局</span></span><br><span class="line"><span class="comment">标题: FLEX 弹性盒模型</span></span><br><span class="line"><span class="comment">标题: MYSQL多表查询随意操作</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h6 id="for-of"><a href="#for-of" class="headerlink" title="for/of"></a>for/of</h6><p>与 <code>for/in</code> 不同的是 <code>for/of</code> 每次循环取其中的值而不是索引。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> lessons = [</span><br><span class="line">	&#123;<span class="attr">title</span>: <span class="string">&#x27;媒体查询响应式布局&#x27;</span>,<span class="attr">category</span>: <span class="string">&#x27;css&#x27;</span>&#125;,</span><br><span class="line">  &#123;<span class="attr">title</span>: <span class="string">&#x27;FLEX 弹性盒模型&#x27;</span>,<span class="attr">category</span>: <span class="string">&#x27;css&#x27;</span>&#125;,</span><br><span class="line">	&#123;<span class="attr">title</span>: <span class="string">&#x27;MYSQL多表查询随意操作&#x27;</span>,<span class="attr">category</span>: <span class="string">&#x27;mysql&#x27;</span>&#125;</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> item <span class="keyword">of</span> lessons) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`</span></span><br><span class="line"><span class="string">    标题: <span class="subst">$&#123;item.title&#125;</span></span></span><br><span class="line"><span class="string">    栏目: <span class="subst">$&#123;item.category == <span class="string">&quot;css&quot;</span> ? <span class="string">&quot;前端&quot;</span> : <span class="string">&quot;数据库&quot;</span>&#125;</span></span></span><br><span class="line"><span class="string">  `</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>







<p>遍历数组的方法包括：every()、filter()、forEach()、map()等。</p>
<p>PS：这几个方法<strong>不会修改原数组</strong>。</p>
<p>语法格式：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">数组/boolean/无 = 数组.<span class="property">every</span>/filter/forEach/map/<span class="title function_">some</span>(</span><br><span class="line">                        <span class="keyword">function</span>(<span class="params">item, index, arr</span>)&#123;</span><br><span class="line">                                        程序和返回值；</span><br><span class="line">                        &#125;)</span><br></pre></td></tr></table></figure>

<h6 id="forEach-遍历"><a href="#forEach-遍历" class="headerlink" title="forEach() 遍历"></a>forEach() 遍历</h6><blockquote>
<p><code>forEach()</code> 这种遍历方法只支持 IE8 以上的浏览器。IE8 及以下的浏览器均不支持该方法。</p>
</blockquote>
<p>forEach()方法需要一个函数作为参数。这种函数，是由我们创建但是不由我们调用的，我们称为回调函数。</p>
<p>数组中有几个元素，该回调函数就会执行几次。</p>
<p>回调函数中传递三个参数：</p>
<ul>
<li>第一个参数，就是当前正在遍历的元素</li>
<li>第二个参数，就是当前正在遍历的元素的索引</li>
<li>第三个参数，就是正在遍历的数组</li>
</ul>
<p>代码举例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> myArr = [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>];</span><br><span class="line"></span><br><span class="line">myArr.<span class="title function_">forEach</span>(<span class="function">(<span class="params">item, index, arr</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;item:&#x27;</span> + item);</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;index:&#x27;</span> + index);</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr));</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;----------&#x27;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">item</span>:a</span><br><span class="line"><span class="attr">index</span>:<span class="number">0</span></span><br><span class="line"><span class="attr">arr</span>:[<span class="string">&quot;王一&quot;</span>,<span class="string">&quot;王二&quot;</span>,<span class="string">&quot;王三&quot;</span>]</span><br><span class="line">----------</span><br><span class="line"><span class="attr">item</span>:b</span><br><span class="line"><span class="attr">index</span>:<span class="number">1</span></span><br><span class="line"><span class="attr">arr</span>:[<span class="string">&quot;王一&quot;</span>,<span class="string">&quot;王二&quot;</span>,<span class="string">&quot;王三&quot;</span>]</span><br><span class="line">----------</span><br><span class="line"><span class="attr">item</span>:c</span><br><span class="line"><span class="attr">index</span>:<span class="number">2</span></span><br><span class="line"><span class="attr">arr</span>:[<span class="string">&quot;王一&quot;</span>,<span class="string">&quot;王二&quot;</span>,<span class="string">&quot;王三&quot;</span>]</span><br><span class="line">----------</span><br></pre></td></tr></table></figure>

<p>注意，forEach() 没有返回值。也可以理解成：forEach() 的返回值是 undefined。如果你尝试 <code>tempArry = myArr.forEach()</code>这种方式来接收，是达不到效果的。</p>
<h6 id="forEach-能不能改变原数组？"><a href="#forEach-能不能改变原数组？" class="headerlink" title="forEach() 能不能改变原数组？"></a>forEach() 能不能改变原数组？</h6><p><strong>1、数组的元素是基本数据类型</strong>：（无法改变原数组）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> numArr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"></span><br><span class="line">numArr.<span class="title function_">forEach</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">    item = item * <span class="number">2</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(numArr)); <span class="comment">// 打印结果：[1, 2, 3]</span></span><br></pre></td></tr></table></figure>

<p><strong>2、数组的元素是引用数据类型</strong>：（直接修改整个元素对象时，无法改变原数组）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> objArr = [</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span> &#125;,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line">objArr.<span class="title function_">forEach</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">    item = &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&#x27;王五&#x27;</span>,</span><br><span class="line">        <span class="attr">age</span>: <span class="string">&#x27;29&#x27;</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(objArr));</span><br><span class="line"><span class="comment">// 打印结果：[&#123;&quot;name&quot;:&quot;张三&quot;,&quot;age&quot;:20&#125;,&#123;&quot;name&quot;:&quot;李四&quot;,&quot;age&quot;:30&#125;]</span></span><br></pre></td></tr></table></figure>

<p><strong>3、数组的元素是引用数据类型</strong>：（修改元素对象里的某个属性时，可以改变原数组）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> objArr = [</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span>&#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span> &#125;,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line">objArr.<span class="title function_">forEach</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">    item.<span class="property">name</span> = <span class="string">&#x27;王五&#x27;</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(objArr)); </span><br><span class="line"><span class="comment">// 打印结果：[&#123;&quot;name&quot;:&quot;王五&quot;,&quot;age&quot;:20&#125;,&#123;&quot;name&quot;:&quot;王五&quot;,&quot;age&quot;:30&#125;]</span></span><br></pre></td></tr></table></figure>

<p><strong>4、forEach() 通过参数 2、参数 3 修改原数组</strong>：（标准做法）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1、数组的元素是基本数据类型</span></span><br><span class="line"><span class="keyword">let</span> numArr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"></span><br><span class="line">numArr.<span class="title function_">forEach</span>(<span class="function">(<span class="params">item, index, arr</span>) =&gt;</span> &#123;</span><br><span class="line">    arr[index] = arr[index] * <span class="number">2</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(numArr)); <span class="comment">// 打印结果：[2,4,6]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 2、数组的元素是引用数据类型时，直接修改对象</span></span><br><span class="line"><span class="keyword">let</span> objArr = [</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">28</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">34</span> &#125;,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line">objArr.<span class="title function_">forEach</span>(<span class="function">(<span class="params">item, index, arr</span>) =&gt;</span> &#123;</span><br><span class="line">    arr[index] = &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&#x27;王五&#x27;</span>,</span><br><span class="line">        <span class="attr">age</span>: <span class="string">&#x27;10&#x27;</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(objArr)); </span><br><span class="line"><span class="comment">// 打印结果：[&#123;&quot;name&quot;:&quot;王五&quot;,&quot;age&quot;:&quot;10&quot;&#125;,&#123;&quot;name&quot;:&quot;王五&quot;,&quot;age&quot;:&quot;10&quot;&#125;]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 3、数组的元素是引用数据类型时，修改对象的某个属性</span></span><br><span class="line"><span class="keyword">let</span> objArr2 = [</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">28</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">34</span> &#125;,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line">objArr2.<span class="title function_">forEach</span>(<span class="function">(<span class="params">item, index, arr</span>) =&gt;</span> &#123;</span><br><span class="line">    arr[index].<span class="property">name</span> = <span class="string">&#x27;王五&#x27;</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(objArr2)); </span><br><span class="line"><span class="comment">// 打印结果：[&#123;&quot;name&quot;:&quot;王五&quot;,&quot;age&quot;:28&#125;,&#123;&quot;name&quot;:&quot;王五&quot;,&quot;age&quot;:34&#125;]</span></span><br></pre></td></tr></table></figure>

<h6 id="map-方法"><a href="#map-方法" class="headerlink" title="map()方法"></a>map()方法</h6><p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">arr.<span class="title function_">map</span>(<span class="keyword">function</span> (<span class="params">item, index, arr</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> newItem;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>解释：对数组中每一项运行回调函数，返回该函数的结果，组成的新数组（返回的是<strong>加工之后</strong>的新数组）。不会改变原数组。</p>
<p>作用：对数组中的每一项进行加工。</p>
<p><strong>举例 1</strong>：（拷贝的过程中改变数组元素的值）</p>
<p>有一个已知的数组 arr1，我要求让 arr1 中的每个元素的值都加 10，这里就可以用到 map 方法。代码举例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr1 = [<span class="number">1</span>, <span class="number">3</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">6</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> arr2 = arr1.<span class="title function_">map</span>(<span class="keyword">function</span> (<span class="params">item, index</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> item + <span class="number">10</span>; <span class="comment">//让arr1中的每个元素加10</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr2);<span class="comment">//(6) [11, 13, 16, 12, 15, 16]</span></span><br></pre></td></tr></table></figure>

<p><strong>举例 2</strong>：【重要案例，实际开发中经常用到】</p>
<p>将 A 数组中某个属性的值，存储到 B 数组中。</p>
<p>代码举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="string">&#x27;28&#x27;</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="string">&#x27;32&#x27;</span> &#125;,</span><br><span class="line">];</span><br><span class="line"><span class="comment">// 将数组 arr1 中的 name 属性，存储到 数组 arr2 中</span></span><br><span class="line"><span class="keyword">const</span> arr2 = arr1.<span class="title function_">map</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> item.<span class="property">name</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 将数组 arr1 中的 name、age这两个属性，改一下“键”的名字，存储到 arr3中</span></span><br><span class="line"><span class="keyword">const</span> arr3 = arr1.<span class="title function_">map</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> (&#123;</span><br><span class="line">    <span class="attr">myName</span>: item.<span class="property">name</span>,</span><br><span class="line">    <span class="attr">myAge</span>: item.<span class="property">age</span>,</span><br><span class="line">&#125;)); <span class="comment">// 将数组 arr1 中的 name 属性，存储到 数组 arr2 中</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr1:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr1));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr2:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr2));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;arr3:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr3));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">arr1</span>:[&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;张三&quot;</span>,<span class="string">&quot;age&quot;</span>:<span class="string">&quot;28&quot;</span>&#125;,&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;李四&quot;</span>,<span class="string">&quot;age&quot;</span>:<span class="string">&quot;32&quot;</span>&#125;]</span><br><span class="line"></span><br><span class="line"><span class="attr">arr2</span>:[<span class="string">&quot;张三&quot;</span>,<span class="string">&quot;李四&quot;</span>]</span><br><span class="line"></span><br><span class="line"><span class="attr">arr3</span>:[&#123;<span class="string">&quot;myName&quot;</span>:<span class="string">&quot;张三&quot;</span>,<span class="string">&quot;myAge&quot;</span>:<span class="string">&quot;28&quot;</span>&#125;,&#123;<span class="string">&quot;myName&quot;</span>:<span class="string">&quot;李四&quot;</span>,<span class="string">&quot;myAge&quot;</span>:<span class="string">&quot;32&quot;</span>&#125;]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h6 id="map-方法会改变原数组吗？"><a href="#map-方法会改变原数组吗？" class="headerlink" title="map() 方法会改变原数组吗？"></a>map() 方法会改变原数组吗？</h6><p><strong>不一定。</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [</span><br><span class="line">       &#123;</span><br><span class="line">         <span class="attr">name</span>: <span class="string">&quot;xiaofeixia&quot;</span>,</span><br><span class="line">         <span class="attr">age</span>: <span class="number">22</span>,</span><br><span class="line">       &#125;,</span><br><span class="line">       &#123;</span><br><span class="line">         <span class="attr">name</span>: <span class="string">&quot;Pan&quot;</span>,</span><br><span class="line">         <span class="attr">age</span>: <span class="number">23</span>,</span><br><span class="line">       &#125;,</span><br><span class="line">     ];</span><br><span class="line"></span><br><span class="line">     arr.<span class="title function_">map</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">       item.<span class="property">name</span> = <span class="string">&quot;haha&quot;</span>; <span class="comment">// 修改 item 里的某个属性</span></span><br><span class="line">       <span class="keyword">return</span> item;</span><br><span class="line">     &#125;);</span><br><span class="line">     <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr));</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;haha&quot;</span>,<span class="string">&quot;age&quot;</span>:<span class="number">22</span>&#125;,&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;haha&quot;</span>,<span class="string">&quot;age&quot;</span>:<span class="number">23</span>&#125;]</span><br></pre></td></tr></table></figure>

<p>总结：<br>map方法如果是修改整个item的值，则不会改变原数组，但如果是修改item里某个属性的值，就会改变原数组。</p>
<h6 id="filter"><a href="#filter" class="headerlink" title="filter()"></a>filter()</h6><p>语法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">arr.<span class="title function_">filter</span>(<span class="keyword">function</span> (<span class="params">item, index, arr</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>解释：对数组中的<strong>每一项</strong>运行回调函数，该函数返回结果是 true 的项，将组成新的数组（返回值就是这个新的数组）。不会改变原数组。</p>
<p>作用：对数组进行过滤。</p>
<p><strong>举例 1</strong>：找出数组 arr1 中大于 4 的元素，返回一个新的数组。代码如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr1 = [<span class="number">1</span>, <span class="number">3</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">6</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> arr2 = arr1.<span class="title function_">filter</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (item &gt; <span class="number">4</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>; <span class="comment">// 将arr1中大于4的元素返回，组成新的数组</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr1)); <span class="comment">// 打印结果：[1,3,6,2,5,6]</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr2)); <span class="comment">// 打印结果：[6,5,6]</span></span><br></pre></td></tr></table></figure>

<p>上方代码更简洁的写法如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr1 = [<span class="number">1</span>, <span class="number">3</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">6</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> arr2 = arr1.<span class="title function_">filter</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> item &gt; <span class="number">4</span>); <span class="comment">// 将arr1中大于4的元素返回，组成新的数组</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr1)); <span class="comment">// 打印结果：[1,3,6,2,5,6]</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr2)); <span class="comment">// 打印结果：[6,5,6]</span></span><br></pre></td></tr></table></figure>



<p><strong>举例 2</strong>：</p>
<p>获取数组 A 中指定类型的对象，放到数组 B 中。</p>
<p>代码举例如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">type</span>: <span class="string">&#x27;男&#x27;</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">type</span>: <span class="string">&#x27;女&#x27;</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&#x27;王五&#x27;</span>, <span class="attr">type</span>: <span class="string">&#x27;男&#x27;</span> &#125;,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> arr2 = arr1.<span class="title function_">filter</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> item.<span class="property">type</span> == <span class="string">&#x27;男&#x27;</span>); <span class="comment">// 筛选出男性</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(arr2));</span><br><span class="line"><span class="comment">//[&#123;&quot;name&quot;:&quot;张三&quot;,&quot;type&quot;:&quot;男&quot;&#125;,&#123;&quot;name&quot;:&quot;王五&quot;,&quot;type&quot;:&quot;男&quot;&#125;]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h6 id="reduce-方法"><a href="#reduce-方法" class="headerlink" title="reduce()方法"></a>reduce()方法</h6><p>reduce() 方法接收一个函数作为累加器，数组中的每个值（从左到右）开始缩减，最终计算为一个值。返回值是回调函数累计处理的结果。</p>
<p><strong>语法</strong>：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">arr.<span class="title function_">reduce</span>(<span class="keyword">function</span> (<span class="params">previousValue, currentValue, currentIndex, arr</span>) &#123;&#125;, initialValue);</span><br></pre></td></tr></table></figure>

<p>参数解释：</p>
<ul>
<li>previousValue：必填，上一次调用回调函数时的返回值</li>
<li>currentValue：必填，当前正在处理的数组元素</li>
<li>currentIndex：选填，当前正在处理的数组元素下标</li>
<li>arr：选填，调用 reduce()方法的数组</li>
<li>initialValue：选填，可选的初始值（作为第一次调用回调函数时传给 previousValue 的值）</li>
</ul>
<p>在以往的数组方法中，匿名的回调函数里是传三个参数：item、index、arr。但是在 reduce() 方法中，前面多传了一个参数<code>previousValue</code>，这个参数的意思是上一次调用回调函数时的返回值。第一次执行回调函数时，previousValue 没有值怎么办？可以用 initialValue 参数传给它。</p>
<p>举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>];</span><br><span class="line"></span><br><span class="line">arr1.<span class="title function_">reduce</span>(<span class="function">(<span class="params">prev, item</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(prev);</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(item);</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;------&#x27;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">88</span>;</span><br><span class="line">&#125;, <span class="number">0</span>);</span><br></pre></td></tr></table></figure>

<p>打印结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">0</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line">------</span><br><span class="line"><span class="number">88</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line">------</span><br><span class="line"><span class="number">88</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line">------</span><br><span class="line"><span class="number">88</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line">------</span><br><span class="line"><span class="number">88</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line">------</span><br><span class="line"><span class="number">88</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line">------</span><br></pre></td></tr></table></figure>

<p>上面的代码中，由于<code>return</code>的是固定值，所以 prev 打印的也是固定值（只有初始值是 0，剩下的遍历中，都是打印 88）。</p>
<h6 id="reduce-的常见应用"><a href="#reduce-的常见应用" class="headerlink" title="reduce() 的常见应用"></a>reduce() 的常见应用</h6><p><strong>举例 1</strong>、求和：</p>
<p>计算数组中所有元素项的总和。代码实现：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">2</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">6</span>];</span><br><span class="line"><span class="comment">// 数组求和</span></span><br><span class="line"><span class="keyword">const</span> total = arr.<span class="title function_">reduce</span>(<span class="function">(<span class="params">prev, item</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> prev + item;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;total:&#x27;</span> + total); <span class="comment">// 打印结果：18</span></span><br></pre></td></tr></table></figure>

<p><strong>举例 2</strong>、统计某个元素出现的次数：</p>
<p>代码实现：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义方法：统一 value 这个元素在数组 arr 中出现的次数</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">repeatCount</span>(<span class="params">arr, value</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!arr || arr.<span class="property">length</span> == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> arr.<span class="title function_">reduce</span>(<span class="function">(<span class="params">totalCount, item</span>) =&gt;</span> &#123;</span><br><span class="line">        totalCount += item == value ? <span class="number">1</span> : <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">return</span> totalCount;</span><br><span class="line">    &#125;, <span class="number">0</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">6</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">6</span>];</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">repeatCount</span>(arr1, <span class="number">6</span>)); <span class="comment">// 打印结果：3</span></span><br></pre></td></tr></table></figure>

<p><strong>举例 3</strong>、求元素的最大值：</p>
<p>代码实现：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">2</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">6</span>];</span><br><span class="line"><span class="comment">// 数组求最大值</span></span><br><span class="line"><span class="keyword">const</span> maxValue = arr.<span class="title function_">reduce</span>(<span class="function">(<span class="params">prev, item</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> prev &gt; item ? prev : item;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(maxValue); <span class="comment">// 打印结果：9</span></span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">潘静怡</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://mengqiuleo.gitee.io/2022/02/26/js/%E6%95%B0%E7%BB%84/">https://mengqiuleo.gitee.io/2022/02/26/js/%E6%95%B0%E7%BB%84/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://mengqiuleo.gitee.io" target="_blank">小飞侠Pan的Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JS/">JS</a><a class="post-meta__tags" href="/tags/%E6%95%B0%E7%BB%84/">数组</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/"><img class="prev-cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">原型与原型链</div></div></a></div><div class="next-post pull-right"><a href="/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/"><img class="next-cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">JS基础之数据类型及其判断</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/" title="原型与原型链"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">原型与原型链</div></div></a></div><div><a href="/2022/02/26/js/call%E5%92%8Cbind%E5%92%8Capply/" title="JS基本语法之call/bind/apply"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">JS基本语法之call/bind/apply</div></div></a></div><div><a href="/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/" title="JS基础之数据类型及其判断"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">JS基础之数据类型及其判断</div></div></a></div><div><a href="/2022/02/26/js/%E7%BB%84%E5%90%88(Compose)%E5%87%BD%E6%95%B0/" title="组合（Compose）函数"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">组合（Compose）函数</div></div></a></div><div><a href="/2022/02/26/js/%E4%BD%9C%E7%94%A8%E5%9F%9F%E4%B8%8E%E5%8F%98%E9%87%8F%E6%8F%90%E5%8D%87/" title="作用域与变量提升"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">作用域与变量提升</div></div></a></div><div><a href="/2022/02/26/js%E5%BC%82%E6%AD%A5/%E5%BC%82%E5%B8%B8error/" title="JS中的异常error处理"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">JS中的异常error处理</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252103634.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">潘静怡</div><div class="author-info__description">一路春光, 一路荆棘, 不虚此行</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">15</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">20</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_52834435?type=blog"><i class="iconfont icon-csdn1"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://blog.csdn.net/weixin_52834435?spm=1000.2115.3001.5343" target="_blank" title="CSDN"><i class="iconfont icon-csdn1"></i></a><a class="social-icon" href="https://gitee.com/mengqiuleo" target="_blank" title="Gitee"><i class="iconfont icon-gitee2"></i></a><a class="social-icon" href="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130203.jpg" target="_blank" title="QQ"><i class="fa-brands fa-qq"></i></a><a class="social-icon" href="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130201.jpg" target="_blank" title="WeChat"><i class="fa-brands fa-weixin"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%80-%E5%88%9B%E5%BB%BA%E6%95%B0%E7%BB%84%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.</span> <span class="toc-text">一.创建数组对象</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E4%BD%BF%E7%94%A8%E5%AD%97%E9%9D%A2%E9%87%8F%E5%88%9B%E5%BB%BA%E6%95%B0%E7%BB%84"><span class="toc-number">1.0.1.</span> <span class="toc-text">方式一：使用字面量创建数组</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E4%BD%BF%E7%94%A8%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E5%88%9B%E5%BB%BA%E6%95%B0%E7%BB%84"><span class="toc-number">1.0.2.</span> <span class="toc-text">方式二：使用构造函数创建数组</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#Array-of"><span class="toc-number">1.0.3.</span> <span class="toc-text">Array.of</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8C-%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E5%85%83%E7%B4%A0%E7%9A%84%E7%B1%BB%E5%9E%8B"><span class="toc-number">2.</span> <span class="toc-text">二.数组中的元素的类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%89-%E6%95%B0%E7%BB%84%E7%9A%84%E5%B8%B8%E8%A7%81%E6%96%B9%E6%B3%95"><span class="toc-number">3.</span> <span class="toc-text">三.数组的常见方法</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E6%95%B0%E7%BB%84%E7%9A%84%E7%B1%BB%E5%9E%8B%E7%9B%B8%E5%85%B3"><span class="toc-number">3.1.</span> <span class="toc-text">1.数组的类型相关</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#Array-from-%EF%BC%9A%E5%B0%86%E4%BC%AA%E6%95%B0%E7%BB%84%E8%BD%AC%E6%8D%A2%E4%B8%BA%E7%9C%9F%E6%95%B0%E7%BB%84"><span class="toc-number">3.1.1.</span> <span class="toc-text">Array.from()：将伪数组转换为真数组</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#Array-of-%EF%BC%9A%E5%88%9B%E5%BB%BA%E6%95%B0%E7%BB%84"><span class="toc-number">3.1.2.</span> <span class="toc-text">Array.of()：创建数组</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E7%9A%84%E6%B7%BB%E5%8A%A0%E5%92%8C%E5%88%A0%E9%99%A4"><span class="toc-number">3.2.</span> <span class="toc-text">2.数组元素的添加和删除</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#slice"><span class="toc-number">3.2.1.</span> <span class="toc-text">slice()</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#splice"><span class="toc-number">3.2.2.</span> <span class="toc-text">splice()</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#fill"><span class="toc-number">3.2.3.</span> <span class="toc-text">fill()</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-%E6%95%B0%E7%BB%84%E7%9A%84%E5%90%88%E5%B9%B6%E5%92%8C%E6%8B%86%E5%88%86"><span class="toc-number">3.3.</span> <span class="toc-text">3.数组的合并和拆分</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#concat"><span class="toc-number">3.3.1.</span> <span class="toc-text">concat()</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#join"><span class="toc-number">3.3.2.</span> <span class="toc-text">join()</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#split"><span class="toc-number">3.3.3.</span> <span class="toc-text">split()</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#copyWithin"><span class="toc-number">3.3.4.</span> <span class="toc-text">copyWithin()</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-%E6%95%B0%E7%BB%84%E6%8E%92%E5%BA%8F"><span class="toc-number">3.4.</span> <span class="toc-text">4.数组排序</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#reverse"><span class="toc-number">3.4.1.</span> <span class="toc-text">reverse()</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#sort-%E6%96%B9%E6%B3%95"><span class="toc-number">3.4.2.</span> <span class="toc-text">sort()方法</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-%E6%9F%A5%E6%89%BE%E6%95%B0%E7%BB%84%E7%9A%84%E5%85%83%E7%B4%A0"><span class="toc-number">3.5.</span> <span class="toc-text">5.查找数组的元素</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#indexOf-%E5%92%8C-lastIndexOf-%EF%BC%9A%E8%8E%B7%E5%8F%96%E6%95%B0%E6%8D%AE%E7%9A%84%E7%B4%A2%E5%BC%95"><span class="toc-number">3.5.1.</span> <span class="toc-text">indexOf() 和 lastIndexOf()：获取数据的索引</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#includes"><span class="toc-number">3.5.2.</span> <span class="toc-text">includes()</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#find"><span class="toc-number">3.5.3.</span> <span class="toc-text">find()</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#findIndex"><span class="toc-number">3.5.4.</span> <span class="toc-text">findIndex()</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#every"><span class="toc-number">3.5.5.</span> <span class="toc-text">every()</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#some"><span class="toc-number">3.5.6.</span> <span class="toc-text">some()</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#6-%E9%81%8D%E5%8E%86%E6%95%B0%E7%BB%84"><span class="toc-number">3.6.</span> <span class="toc-text">6.遍历数组</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#for-in"><span class="toc-number">3.6.1.</span> <span class="toc-text">for&#x2F;in</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#for-of"><span class="toc-number">3.6.2.</span> <span class="toc-text">for&#x2F;of</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#forEach-%E9%81%8D%E5%8E%86"><span class="toc-number">3.6.3.</span> <span class="toc-text">forEach() 遍历</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#forEach-%E8%83%BD%E4%B8%8D%E8%83%BD%E6%94%B9%E5%8F%98%E5%8E%9F%E6%95%B0%E7%BB%84%EF%BC%9F"><span class="toc-number">3.6.4.</span> <span class="toc-text">forEach() 能不能改变原数组？</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#map-%E6%96%B9%E6%B3%95"><span class="toc-number">3.6.5.</span> <span class="toc-text">map()方法</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#map-%E6%96%B9%E6%B3%95%E4%BC%9A%E6%94%B9%E5%8F%98%E5%8E%9F%E6%95%B0%E7%BB%84%E5%90%97%EF%BC%9F"><span class="toc-number">3.6.6.</span> <span class="toc-text">map() 方法会改变原数组吗？</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#filter"><span class="toc-number">3.6.7.</span> <span class="toc-text">filter()</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#reduce-%E6%96%B9%E6%B3%95"><span class="toc-number">3.6.8.</span> <span class="toc-text">reduce()方法</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#reduce-%E7%9A%84%E5%B8%B8%E8%A7%81%E5%BA%94%E7%94%A8"><span class="toc-number">3.6.9.</span> <span class="toc-text">reduce() 的常见应用</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/ajax/ajax/" title="JS异步之ajax"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS异步之ajax"/></a><div class="content"><a class="title" href="/2022/02/26/ajax/ajax/" title="JS异步之ajax">JS异步之ajax</a><time datetime="2022-02-26T10:53:56.362Z" title="发表于 2022-02-26 18:53:56">2022-02-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/js/call%E5%92%8Cbind%E5%92%8Capply/" title="JS基本语法之call/bind/apply"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS基本语法之call/bind/apply"/></a><div class="content"><a class="title" href="/2022/02/26/js/call%E5%92%8Cbind%E5%92%8Capply/" title="JS基本语法之call/bind/apply">JS基本语法之call/bind/apply</a><time datetime="2022-02-26T10:52:29.044Z" title="发表于 2022-02-26 18:52:29">2022-02-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/" title="JS基础之数据类型及其判断"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS基础之数据类型及其判断"/></a><div class="content"><a class="title" href="/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/" title="JS基础之数据类型及其判断">JS基础之数据类型及其判断</a><time datetime="2022-02-26T10:49:21.438Z" title="发表于 2022-02-26 18:49:21">2022-02-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/js/%E6%95%B0%E7%BB%84/" title="JS基本语法之数组"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS基本语法之数组"/></a><div class="content"><a class="title" href="/2022/02/26/js/%E6%95%B0%E7%BB%84/" title="JS基本语法之数组">JS基本语法之数组</a><time datetime="2022-02-26T10:47:39.357Z" title="发表于 2022-02-26 18:47:39">2022-02-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/" title="原型与原型链"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="原型与原型链"/></a><div class="content"><a class="title" href="/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/" title="原型与原型链">原型与原型链</a><time datetime="2022-02-26T10:42:32.957Z" title="发表于 2022-02-26 18:42:32">2022-02-26</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By 潘静怡</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">welcome to my blog!🤪</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (true){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: '',
      appKey: '',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script src="/js/mobile.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start -->
  <script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/hexo-github-calendar@1.21/hexo_githubcalendar.js"></script>
  <script data-pjax>
        function GithubCalendarConfig(){
            var git_githubapiurl ="https://python-gitee-calendar-api.vercel.app/api?mengqiuleo";
            var git_color =['#ebedf0', '#f0fff4', '#dcffe4', '#bef5cb', '#85e89d', '#34d058', '#28a745', '#22863a', '#176f2c', '#165c26', '#144620'];
            var git_user ="mengqiuleo";
            var parent_div_git = document.getElementById('recent-posts');
            var git_div_html = '<div class="recent-post-item" style="width:100%;height:auto;padding:10px;"><div id="github_loading" style="width:10%;height:100%;margin:0 auto;display: block"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  viewBox="0 0 50 50" style="enable-background:new 0 0 50 50" xml:space="preserve"><path fill="#d0d0d0" d="M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z" transform="rotate(275.098 25 25)"><animateTransform attributeType="xml" attributeName="transform" type="rotate" from="0 25 25" to="360 25 25" dur="0.6s" repeatCount="indefinite"></animateTransform></path></svg></div><div id="github_container"></div></div>';
            if(parent_div_git && location.pathname =='/'){
                console.log('已挂载github calendar')
                // parent_div_git.innerHTML=git_div_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",git_div_html) // 有报错，但不影响使用(支持pjax跳转)
            };
            GithubCalendar(git_githubapiurl,git_color,git_user)
        }
        if(document.getElementById('recent-posts')){
            GithubCalendarConfig()
        }
    </script>
    <style>#github_container{min-height:280px}@media screen and (max-width:650px) {#github_container{background-image:;min-height:0px}}</style>
    <style></style><!-- hexo injector body_end end --></body></html>